package com.firsteapps.mobile.android.puzzle;

import java.io.File;
import java.util.HashMap;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.LayoutParams;

import com.firsteapps.mobile.android.puzzle.game.Helper;
import com.firsteapps.mobile.android.puzzle.util.Consts;
import com.firsteapps.mobile.android.puzzle.util.Utilities;

public class SelectProfile extends BaseActivity {
	RadioGroup usernames;
	ImageView deleteProfile;

	private HashMap<Integer, File> files;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_profile);

		usernames = (RadioGroup) findViewById(R.id.users_list);
		files = new HashMap<Integer, File>();
		listUsers();

		findViewById(R.id.selpr_create).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Utilities.startActivityWithAnimation(SelectProfile.this, CreateProfile.class);
			}
		});

		findViewById(R.id.selpr_select).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int id = usernames.getCheckedRadioButtonId();
				String user = null;
				if (id != -1 && (user = files.get(id).getName()) != null) {
					setUserName(user);
					Utilities.startActivityWithAnimation(SelectProfile.this, PostLogin.class);
				} else
					Helper.doMessageDialog(SelectProfile.this, "Select Profile", "No user is selected");
			}
		});

		deleteProfile = (ImageView) findViewById(R.id.delete_profile);
		deleteProfile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final int id = usernames.getCheckedRadioButtonId();
				if (id != -1 && files.get(id) != null) {
					Helper.doMessageDialog(SelectProfile.this, "Delete Profile", "Are you sure you want to delete the profile?",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									if (Helper.deleteFolder(files.get(id))) {
										System.out.println("Profile successfully deleted");
										listUsers();
									} else
										Helper.doMessageDialog(SelectProfile.this, "Select Profile",
												"Unable to delete the profile");
								}
							}, true);
				} else
					Helper.doMessageDialog(SelectProfile.this, "Select Profile", "No user is selected");
			}
		});
	}

	private void listUsers() {
		File usersDir = new File(getFilesDir() + "/" + Consts.USERS_FOLDER);
		File[] users = usersDir.listFiles();
		usernames.removeAllViews();
		usernames.clearCheck();
		files.clear();

		if (users != null) {
			for (int i = 0; i < users.length; i++) {
				RadioButton user = new RadioButton(this);
				user.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				String name = null;
				if (users[i].isDirectory() && (name = Helper.getUserName(users[i].getName())) != null) {
					user.setText(name);
					
					DisplayMetrics displayMetrics = new DisplayMetrics();
					getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
					
					if (displayMetrics.widthPixels == 480)
						user.setTextAppearance(SelectProfile.this, R.style.Text2);
					else if (displayMetrics.widthPixels == 800)
						user.setTextAppearance(SelectProfile.this, R.style.Text1);
					else if (displayMetrics.widthPixels == 960)
						user.setTextAppearance(SelectProfile.this, R.style.Text2);
					else if (displayMetrics.widthPixels == 1280)
					{
						if (displayMetrics.heightPixels == 800)
							user.setTextAppearance(SelectProfile.this, R.style.Text);
						else if (displayMetrics.heightPixels == 720)
							user.setTextAppearance(SelectProfile.this, R.style.Text1);
					}
					else
						user.setTextAppearance(SelectProfile.this, R.style.Text);
					
					usernames.addView(user);
					files.put(user.getId(), users[i]);
				}
			}
		}

		usernames.invalidate();
	}
}
